import Vue from 'vue'
import VueRouter from 'vue-router'
import Main from "@/views/Main.vue";

Vue.use(VueRouter)

const routes = [{
  path: '/',
  name: 'Main',
  component: Main,
  redirect: "/Pandect",
  children: [{
    path: 'Pandect',
    name: 'Pandect',
    meta: {
      title: '总览'
    },
    component: () => import('../views/Pandect.vue')
  }, {
    path: '/projectApproval',
    name: 'projectApproval',
    meta: {
      title: '施工立项'
    },
    component: () => import('../views/construction/projectApproval.vue')
  }, {
    path: '/projectApprovalAU',
    name: 'projectApprovalAU',
    component: () => import('../views/construction/projectApprovalAU.vue')
  }, {
    path: '/ProjectDisclosure',
    name: 'ProjectDisclosure',
    meta: {
      title: '项目交底'
    },
    component: () => import('../views/construction/ProjectDisclosure.vue')
  }, {
    path: '/ProjectDisclosureAU',
    name: 'ProjectDisclosureAU',
    component: () => import('../views/construction/ProjectDisclosureAU.vue')
  }]
}, {
  path: '/Login',
  name: 'Login',
  meta: {
    title: '登录'
  },
  component: () => import('../views/Login.vue')
}]

const router = new VueRouter({
  routes
})

export default router
//前置路由守卫
router.beforeEach((to, from, next) => {
  if (to.path === '/Login') {
    next();
  } else {

    const user = window.sessionStorage.getItem("user");
    const token = user ? JSON.parse(user).token : null;
    if (!token && to.path !== '/Login') {
      window.alert("请先登录");
      next('/Login');
    }

    next()
  }

})